2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
11 #define BLOCKING (1ul << 14)
12 #define ORIG_OFFSET (40)
15 * Leave space at the beginning to insert the tag
16 * once we know how big things are.
18 * FIXME! Share the code with "write-tree.c"
20 static void init_buffer(char **bufp
, unsigned int *sizep
)
22 char *buf
= malloc(BLOCKING
);
23 memset(buf
, 0, ORIG_OFFSET
);
28 static void add_buffer(char **bufp
, unsigned int *sizep
, const char *fmt
, ...)
33 unsigned long alloc
, size
, newsize
;
37 len
= vsnprintf(one_line
, sizeof(one_line
), fmt
, args
);
41 alloc
= (size
+ 32767) & ~32767;
43 if (newsize
> alloc
) {
44 alloc
= (newsize
+ 32767) & ~32767;
45 buf
= realloc(buf
, alloc
);
49 memcpy(buf
+ size
, one_line
, len
);
52 static int prepend_integer(char *buffer
, unsigned val
, int i
)
56 buffer
[--i
] = '0' + (val
% 10);
62 static void finish_buffer(char *tag
, char **bufp
, unsigned int *sizep
)
67 unsigned int size
= *sizep
;
69 offset
= prepend_integer(buf
, size
- ORIG_OFFSET
, ORIG_OFFSET
);
74 memcpy(buf
, tag
, taglen
);
80 static void remove_special(char *p
)
89 case '\n': case '<': case '>':
99 * Having more than two parents may be strange, but hey, there's
100 * no conceptual reason why the file format couldn't accept multi-way
101 * merges. It might be the "union" of several packages, for example.
103 * I don't really expect that to happen, but this is here to make
104 * it clear that _conceptually_ it's ok..
106 #define MAXPARENT (16)
108 int main(int argc
, char **argv
)
112 unsigned char tree_sha1
[20];
113 unsigned char parent_sha1
[MAXPARENT
][20];
114 char *gecos
, *realgecos
;
115 char *email
, realemail
[1000];
116 char *date
, *realdate
;
123 if (argc
< 2 || get_sha1_hex(argv
[1], tree_sha1
) < 0)
124 usage("commit-tree <sha1> [-p <sha1>]* < changelog");
126 for (i
= 2; i
< argc
; i
+= 2) {
128 a
= argv
[i
]; b
= argv
[i
+1];
129 if (!b
|| strcmp(a
, "-p") || get_sha1_hex(b
, parent_sha1
[parents
]))
130 usage("commit-tree <sha1> [-p <sha1>]* < changelog");
134 fprintf(stderr
, "Committing initial tree %s\n", argv
[1]);
135 pw
= getpwuid(getuid());
137 usage("You don't exist. Go away!");
138 realgecos
= pw
->pw_gecos
;
139 len
= strlen(pw
->pw_name
);
140 memcpy(realemail
, pw
->pw_name
, len
);
141 realemail
[len
] = '@';
142 gethostname(realemail
+len
+1, sizeof(realemail
)-len
-1);
144 realdate
= ctime(&now
);
146 gecos
= getenv("COMMITTER_NAME") ? : realgecos
;
147 email
= getenv("COMMITTER_EMAIL") ? : realemail
;
148 date
= getenv("COMMITTER_DATE") ? : realdate
;
150 remove_special(gecos
); remove_special(realgecos
);
151 remove_special(email
); remove_special(realemail
);
152 remove_special(date
); remove_special(realdate
);
154 init_buffer(&buffer
, &size
);
155 add_buffer(&buffer
, &size
, "tree %s\n", sha1_to_hex(tree_sha1
));
158 * NOTE! This ordering means that the same exact tree merged with a
159 * different order of parents will be a _different_ changeset even
160 * if everything else stays the same.
162 for (i
= 0; i
< parents
; i
++)
163 add_buffer(&buffer
, &size
, "parent %s\n", sha1_to_hex(parent_sha1
[i
]));
165 /* Person/date information */
166 add_buffer(&buffer
, &size
, "author %s <%s> %s\n", gecos
, email
, date
);
167 add_buffer(&buffer
, &size
, "committer %s <%s> %s\n\n", realgecos
, realemail
, realdate
);
169 /* And add the comment */
170 while (fgets(comment
, sizeof(comment
), stdin
) != NULL
)
171 add_buffer(&buffer
, &size
, "%s", comment
);
173 finish_buffer("commit ", &buffer
, &size
);
175 write_sha1_file(buffer
, size
);