Make git-write-tree a builtin
Signed-off-by: Lukas Sandström <lukass@etek.chalmers.se> Signed-off-by: Junio C Hamano <junkio@cox.net>
Lukas Sandström committed
Jun 13, 2006 at 22:21 UTC
8ed05fb5e90f8ad2051ff7071b7425a53c594892
4 files changed
+50
-30
Makefile
+3
-3
@@ -159,7 +159,7 @@ PROGRAMS = \
159
git-show-index$X git-ssh-fetch$X \
160
git-ssh-upload$X git-unpack-file$X \
161
git-unpack-objects$X git-update-index$X git-update-server-info$X \
162
- git-upload-pack$X git-verify-pack$X git-write-tree$X \
162
+ git-upload-pack$X git-verify-pack$X \
163
git-update-ref$X git-symbolic-ref$X \
164
git-name-rev$X git-pack-redundant$X git-repo-config$X git-var$X \
165
git-describe$X git-merge-tree$X git-blame$X git-imap-send$X
@@ -170,7 +170,7 @@ BUILT_INS = git-log$X git-whatchanged$X git-show$X \
170
git-check-ref-format$X git-rev-parse$X \
171
git-init-db$X git-tar-tree$X git-upload-tar$X git-format-patch$X \
172
git-ls-files$X git-ls-tree$X git-get-tar-commit-id$X \
173
- git-read-tree$X git-commit-tree$X \
173
+ git-read-tree$X git-commit-tree$X git-write-tree$X \
174
git-apply$X git-show-branch$X git-diff-files$X \
175
git-diff-index$X git-diff-stages$X git-diff-tree$X git-cat-file$X
176
@@ -223,7 +223,7 @@ BUILTIN_OBJS = \
223
builtin-grep.o builtin-add.o builtin-rev-list.o builtin-check-ref-format.o \
224
builtin-rm.o builtin-init-db.o builtin-rev-parse.o \
225
builtin-tar-tree.o builtin-upload-tar.o \
226
- builtin-ls-files.o builtin-ls-tree.o \
226
+ builtin-ls-files.o builtin-ls-tree.o builtin-write-tree.o \
227
builtin-read-tree.o builtin-commit-tree.o \
228
builtin-apply.o builtin-show-branch.o builtin-diff-files.o \
229
builtin-diff-index.o builtin-diff-stages.o builtin-diff-tree.o \
builtin-write-tree.c
renamed
+42
-26
@@ -3,41 +3,24 @@
3
*
4
* Copyright (C) Linus Torvalds, 2005
5
*/
6
+#include "builtin.h"
7
#include "cache.h"
8
#include "tree.h"
9
#include "cache-tree.h"
10
10
-static int missing_ok = 0;
11
-static char *prefix = NULL;
12
-
11
static const char write_tree_usage[] =
12
"git-write-tree [--missing-ok] [--prefix=<prefix>/]";
13
16
-static struct lock_file lock_file;
17
-
18
-int main(int argc, char **argv)
14
+int write_tree(unsigned char *sha1, int missing_ok, const char *prefix)
15
{
16
int entries, was_valid, newfd;
17
22
- setup_git_directory();
18
+ /* We can't free this memory, it becomes part of a linked list parsed atexit() */
19
+ struct lock_file *lock_file = xmalloc(sizeof(struct lock_file));
20
24
- newfd = hold_lock_file_for_update(&lock_file, get_index_file());
25
- entries = read_cache();
26
-
27
- while (1 < argc) {
28
- char *arg = argv[1];
29
- if (!strcmp(arg, "--missing-ok"))
30
- missing_ok = 1;
31
- else if (!strncmp(arg, "--prefix=", 9))
32
- prefix = arg + 9;
33
- else
34
- die(write_tree_usage);
35
- argc--; argv++;
36
- }
37
-
38
- if (argc > 2)
39
- die("too many options");
21
+ newfd = hold_lock_file_for_update(lock_file, get_index_file());
22
23
+ entries = read_cache();
24
if (entries < 0)
25
die("git-write-tree: error reading cache");
26
@@ -45,6 +28,7 @@ int main(int argc, char **argv)
28
active_cache_tree = cache_tree();
29
30
was_valid = cache_tree_fully_valid(active_cache_tree);
31
+
32
if (!was_valid) {
33
if (cache_tree_update(active_cache_tree,
34
active_cache, active_nr,
@@ -52,7 +36,7 @@ int main(int argc, char **argv)
36
die("git-write-tree: error building trees");
37
if (0 <= newfd) {
38
if (!write_cache(newfd, active_cache, active_nr))
55
- commit_lock_file(&lock_file);
39
+ commit_lock_file(lock_file);
40
}
41
/* Not being able to write is fine -- we are only interested
42
* in updating the cache-tree part, and if the next caller
@@ -61,12 +45,44 @@ int main(int argc, char **argv)
45
* performance penalty and not a big deal.
46
*/
47
}
48
+
49
if (prefix) {
50
struct cache_tree *subtree =
51
cache_tree_find(active_cache_tree, prefix);
67
- printf("%s\n", sha1_to_hex(subtree->sha1));
52
+ memcpy(sha1, subtree->sha1, 20);
53
}
54
else
70
- printf("%s\n", sha1_to_hex(active_cache_tree->sha1));
55
+ memcpy(sha1, active_cache_tree->sha1, 20);
56
+
57
+ rollback_lock_file(lock_file);
58
+
59
return 0;
60
}
61
+
62
+int cmd_write_tree(int argc, const char **argv, char **envp)
63
+{
64
+ int missing_ok = 0, ret;
65
+ const char *prefix = NULL;
66
+ unsigned char sha1[20];
67
+
68
+ setup_git_directory();
69
+
70
+ while (1 < argc) {
71
+ const char *arg = argv[1];
72
+ if (!strcmp(arg, "--missing-ok"))
73
+ missing_ok = 1;
74
+ else if (!strncmp(arg, "--prefix=", 9))
75
+ prefix = arg + 9;
76
+ else
77
+ die(write_tree_usage);
78
+ argc--; argv++;
79
+ }
80
+
81
+ if (argc > 2)
82
+ die("too many options");
83
+
84
+ ret = write_tree(sha1, missing_ok, prefix);
85
+ printf("%s\n", sha1_to_hex(sha1));
86
+
87
+ return ret;
88
+}
builtin.h
+3
@@ -46,4 +46,7 @@ extern int cmd_diff_tree(int argc, const char **argv, char **envp);
46
extern int cmd_cat_file(int argc, const char **argv, char **envp);
47
extern int cmd_rev_parse(int argc, const char **argv, char **envp);
48
49
+extern int cmd_write_tree(int argc, const char **argv, char **envp);
50
+extern int write_tree(unsigned char *sha1, int missing_ok, const char *prefix);
51
+
52
#endif
git.c
+2
-1
@@ -178,7 +178,8 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
178
{ "diff-stages", cmd_diff_stages },
179
{ "diff-tree", cmd_diff_tree },
180
{ "cat-file", cmd_cat_file },
181
- { "rev-parse", cmd_rev_parse }
181
+ { "rev-parse", cmd_rev_parse },
182
+ { "write-tree", cmd_write_tree }
183
};
184
int i;
185