2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
12 #define BLOCKING (1ul << 14)
15 * FIXME! Share the code with "write-tree.c"
17 static void init_buffer(char **bufp
, unsigned int *sizep
)
19 char *buf
= xmalloc(BLOCKING
);
24 static void add_buffer(char **bufp
, unsigned int *sizep
, const char *fmt
, ...)
29 unsigned long alloc
, size
, newsize
;
33 len
= vsnprintf(one_line
, sizeof(one_line
), fmt
, args
);
37 alloc
= (size
+ 32767) & ~32767;
39 if (newsize
> alloc
) {
40 alloc
= (newsize
+ 32767) & ~32767;
41 buf
= xrealloc(buf
, alloc
);
45 memcpy(buf
+ size
, one_line
, len
);
48 static void remove_special(char *p
)
51 char *dst
= p
, *src
= p
;
57 case '\n': case '<': case '>':
66 * Go back, and remove crud from the end: some people
67 * have commas etc in their gecos field
71 unsigned char c
= *dst
;
73 case ',': case ';': case '.':
81 static void check_valid(unsigned char *sha1
, const char *expect
)
87 buf
= read_sha1_file(sha1
, type
, &size
);
88 if (!buf
|| strcmp(type
, expect
))
89 die("%s is not a valid '%s' object", sha1_to_hex(sha1
), expect
);
94 * Having more than two parents is not strange at all, and this is
95 * how multi-way merges are represented.
97 #define MAXPARENT (16)
98 static unsigned char parent_sha1
[MAXPARENT
][20];
100 static char *commit_tree_usage
= "git-commit-tree <sha1> [-p <sha1>]* < changelog";
102 static int new_parent(int idx
)
105 unsigned char *sha1
= parent_sha1
[idx
];
106 for (i
= 0; i
< idx
; i
++) {
107 if (!memcmp(parent_sha1
[i
], sha1
, 20)) {
108 error("duplicate parent %s ignored", sha1_to_hex(sha1
));
115 int main(int argc
, char **argv
)
119 unsigned char tree_sha1
[20];
120 unsigned char commit_sha1
[20];
121 char *gecos
, *realgecos
, *commitgecos
;
122 char *email
, *commitemail
, realemail
[1000];
123 char date
[50], realdate
[50];
124 char *audate
, *cmdate
;
130 if (argc
< 2 || get_sha1_hex(argv
[1], tree_sha1
) < 0)
131 usage(commit_tree_usage
);
133 check_valid(tree_sha1
, "tree");
134 for (i
= 2; i
< argc
; i
+= 2) {
136 a
= argv
[i
]; b
= argv
[i
+1];
137 if (!b
|| strcmp(a
, "-p") || get_sha1(b
, parent_sha1
[parents
]))
138 usage(commit_tree_usage
);
139 check_valid(parent_sha1
[parents
], "commit");
140 if (new_parent(parents
))
144 fprintf(stderr
, "Committing initial tree %s\n", argv
[1]);
145 pw
= getpwuid(getuid());
147 die("You don't exist. Go away!");
148 realgecos
= pw
->pw_gecos
;
149 len
= strlen(pw
->pw_name
);
150 memcpy(realemail
, pw
->pw_name
, len
);
151 realemail
[len
] = '@';
152 gethostname(realemail
+len
+1, sizeof(realemail
)-len
-1);
153 if (!strchr(realemail
+len
+1, '.')) {
154 strcat(realemail
, ".");
155 getdomainname(realemail
+strlen(realemail
), sizeof(realemail
)-strlen(realemail
)-1);
158 datestamp(realdate
, sizeof(realdate
));
159 strcpy(date
, realdate
);
161 commitgecos
= gitenv("GIT_COMMITTER_NAME") ? : realgecos
;
162 commitemail
= gitenv("GIT_COMMITTER_EMAIL") ? : realemail
;
163 gecos
= gitenv("GIT_AUTHOR_NAME") ? : realgecos
;
164 email
= gitenv("GIT_AUTHOR_EMAIL") ? : realemail
;
165 audate
= gitenv("GIT_AUTHOR_DATE");
167 parse_date(audate
, date
, sizeof(date
));
168 cmdate
= gitenv("GIT_COMMITTER_DATE");
170 parse_date(cmdate
, realdate
, sizeof(realdate
));
172 remove_special(gecos
); remove_special(realgecos
); remove_special(commitgecos
);
173 remove_special(email
); remove_special(realemail
); remove_special(commitemail
);
175 init_buffer(&buffer
, &size
);
176 add_buffer(&buffer
, &size
, "tree %s\n", sha1_to_hex(tree_sha1
));
179 * NOTE! This ordering means that the same exact tree merged with a
180 * different order of parents will be a _different_ changeset even
181 * if everything else stays the same.
183 for (i
= 0; i
< parents
; i
++)
184 add_buffer(&buffer
, &size
, "parent %s\n", sha1_to_hex(parent_sha1
[i
]));
186 /* Person/date information */
187 add_buffer(&buffer
, &size
, "author %s <%s> %s\n", gecos
, email
, date
);
188 add_buffer(&buffer
, &size
, "committer %s <%s> %s\n\n", commitgecos
, commitemail
, realdate
);
190 /* And add the comment */
191 while (fgets(comment
, sizeof(comment
), stdin
) != NULL
)
192 add_buffer(&buffer
, &size
, "%s", comment
);
194 write_sha1_file(buffer
, size
, "commit", commit_sha1
);
195 printf("%s\n", sha1_to_hex(commit_sha1
));