]>
Commit | Line | Data |
---|---|---|
8bc9a0c7 LT |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | */ | |
8ed05fb5 | 6 | #include "builtin.h" |
e83c5163 | 7 | #include "cache.h" |
8e440259 | 8 | #include "tree.h" |
a52139b4 | 9 | #include "cache-tree.h" |
e83c5163 | 10 | |
6bd20358 JH |
11 | static const char write_tree_usage[] = |
12 | "git-write-tree [--missing-ok] [--prefix=<prefix>/]"; | |
75a46f6b | 13 | |
8ed05fb5 | 14 | int write_tree(unsigned char *sha1, int missing_ok, const char *prefix) |
d6d3f9d0 | 15 | { |
bad68ec9 | 16 | int entries, was_valid, newfd; |
a52139b4 | 17 | |
8ed05fb5 | 18 | /* We can't free this memory, it becomes part of a linked list parsed atexit() */ |
928e47e3 | 19 | struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file)); |
53228a5f | 20 | |
30ca07a2 | 21 | newfd = hold_locked_index(lock_file, 1); |
d6d3f9d0 | 22 | |
8ed05fb5 | 23 | entries = read_cache(); |
c899350e | 24 | if (entries < 0) |
667bb59b | 25 | die("git-write-tree: error reading cache"); |
c347ea5d | 26 | |
bad68ec9 JH |
27 | if (!active_cache_tree) |
28 | active_cache_tree = cache_tree(); | |
29 | ||
30 | was_valid = cache_tree_fully_valid(active_cache_tree); | |
8ed05fb5 | 31 | |
bad68ec9 JH |
32 | if (!was_valid) { |
33 | if (cache_tree_update(active_cache_tree, | |
34 | active_cache, active_nr, | |
2956dd3b | 35 | missing_ok, 0) < 0) |
bad68ec9 JH |
36 | die("git-write-tree: error building trees"); |
37 | if (0 <= newfd) { | |
6244b249 | 38 | if (!write_cache(newfd, active_cache, active_nr) |
c21aa54e | 39 | && !close(newfd)) { |
8ed05fb5 | 40 | commit_lock_file(lock_file); |
c21aa54e AR |
41 | newfd = -1; |
42 | } | |
bad68ec9 JH |
43 | } |
44 | /* Not being able to write is fine -- we are only interested | |
45 | * in updating the cache-tree part, and if the next caller | |
46 | * ends up using the old index with unupdated cache-tree part | |
47 | * it misses the work we did here, but that is just a | |
48 | * performance penalty and not a big deal. | |
49 | */ | |
50 | } | |
8ed05fb5 | 51 | |
6bd20358 JH |
52 | if (prefix) { |
53 | struct cache_tree *subtree = | |
54 | cache_tree_find(active_cache_tree, prefix); | |
b87841e1 NTND |
55 | if (!subtree) |
56 | die("git-write-tree: prefix %s not found", prefix); | |
e702496e | 57 | hashcpy(sha1, subtree->sha1); |
6bd20358 JH |
58 | } |
59 | else | |
e702496e | 60 | hashcpy(sha1, active_cache_tree->sha1); |
8ed05fb5 | 61 | |
c21aa54e AR |
62 | if (0 <= newfd) |
63 | close(newfd); | |
8ed05fb5 LS |
64 | rollback_lock_file(lock_file); |
65 | ||
e83c5163 LT |
66 | return 0; |
67 | } | |
8ed05fb5 | 68 | |
a633fca0 | 69 | int cmd_write_tree(int argc, const char **argv, const char *unused_prefix) |
8ed05fb5 LS |
70 | { |
71 | int missing_ok = 0, ret; | |
72 | const char *prefix = NULL; | |
73 | unsigned char sha1[20]; | |
74 | ||
9ec398d2 | 75 | git_config(git_default_config); |
8ed05fb5 LS |
76 | while (1 < argc) { |
77 | const char *arg = argv[1]; | |
78 | if (!strcmp(arg, "--missing-ok")) | |
79 | missing_ok = 1; | |
cc44c765 | 80 | else if (!prefixcmp(arg, "--prefix=")) |
8ed05fb5 LS |
81 | prefix = arg + 9; |
82 | else | |
8cdf3364 | 83 | usage(write_tree_usage); |
8ed05fb5 LS |
84 | argc--; argv++; |
85 | } | |
86 | ||
87 | if (argc > 2) | |
88 | die("too many options"); | |
89 | ||
90 | ret = write_tree(sha1, missing_ok, prefix); | |
91 | printf("%s\n", sha1_to_hex(sha1)); | |
92 | ||
93 | return ret; | |
94 | } |