2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
14 #define BLOCKING (1ul << 14)
17 * FIXME! Share the code with "write-tree.c"
19 static void init_buffer(char **bufp
, unsigned int *sizep
)
21 char *buf
= xmalloc(BLOCKING
);
26 static void add_buffer(char **bufp
, unsigned int *sizep
, const char *fmt
, ...)
31 unsigned long alloc
, size
, newsize
;
35 len
= vsnprintf(one_line
, sizeof(one_line
), fmt
, args
);
39 alloc
= (size
+ 32767) & ~32767;
41 if (newsize
> alloc
) {
42 alloc
= (newsize
+ 32767) & ~32767;
43 buf
= xrealloc(buf
, alloc
);
47 memcpy(buf
+ size
, one_line
, len
);
50 static void remove_special(char *p
)
53 char *dst
= p
, *src
= p
;
59 case '\n': case '<': case '>':
68 * Go back, and remove crud from the end: some people
69 * have commas etc in their gecos field
73 unsigned char c
= *dst
;
75 case ',': case ';': case '.':
83 static void check_valid(unsigned char *sha1
, const char *expect
)
89 buf
= read_sha1_file(sha1
, type
, &size
);
90 if (!buf
|| strcmp(type
, expect
))
91 die("%s is not a valid '%s' object", sha1_to_hex(sha1
), expect
);
96 * Having more than two parents is not strange at all, and this is
97 * how multi-way merges are represented.
99 #define MAXPARENT (16)
101 static char *commit_tree_usage
= "git-commit-tree <sha1> [-p <sha1>]* < changelog";
103 int main(int argc
, char **argv
)
107 unsigned char tree_sha1
[20];
108 unsigned char parent_sha1
[MAXPARENT
][20];
109 unsigned char commit_sha1
[20];
110 char *gecos
, *realgecos
, *commitgecos
;
111 char *email
, *commitemail
, realemail
[1000];
112 char date
[20], realdate
[20];
119 if (argc
< 2 || get_sha1_hex(argv
[1], tree_sha1
) < 0)
120 usage(commit_tree_usage
);
122 check_valid(tree_sha1
, "tree");
123 for (i
= 2; i
< argc
; i
+= 2) {
125 a
= argv
[i
]; b
= argv
[i
+1];
126 if (!b
|| strcmp(a
, "-p") || get_sha1(b
, parent_sha1
[parents
]))
127 usage(commit_tree_usage
);
128 check_valid(parent_sha1
[parents
], "commit");
132 fprintf(stderr
, "Committing initial tree %s\n", argv
[1]);
133 pw
= getpwuid(getuid());
135 die("You don't exist. Go away!");
136 realgecos
= pw
->pw_gecos
;
137 len
= strlen(pw
->pw_name
);
138 memcpy(realemail
, pw
->pw_name
, len
);
139 realemail
[len
] = '@';
140 gethostname(realemail
+len
+1, sizeof(realemail
)-len
-1);
141 if (!strchr(realemail
+len
+1, '.')) {
142 strcat(realemail
, ".");
143 getdomainname(realemail
+strlen(realemail
), sizeof(realemail
)-strlen(realemail
)-1);
146 datestamp(realdate
, sizeof(realdate
));
147 strcpy(date
, realdate
);
149 commitgecos
= gitenv("GIT_COMMITTER_NAME") ? : realgecos
;
150 commitemail
= gitenv("GIT_COMMITTER_EMAIL") ? : realemail
;
151 gecos
= gitenv("GIT_AUTHOR_NAME") ? : realgecos
;
152 email
= gitenv("GIT_AUTHOR_EMAIL") ? : realemail
;
153 audate
= gitenv("GIT_AUTHOR_DATE");
155 parse_date(audate
, date
, sizeof(date
));
157 remove_special(gecos
); remove_special(realgecos
); remove_special(commitgecos
);
158 remove_special(email
); remove_special(realemail
); remove_special(commitemail
);
160 init_buffer(&buffer
, &size
);
161 add_buffer(&buffer
, &size
, "tree %s\n", sha1_to_hex(tree_sha1
));
164 * NOTE! This ordering means that the same exact tree merged with a
165 * different order of parents will be a _different_ changeset even
166 * if everything else stays the same.
168 for (i
= 0; i
< parents
; i
++)
169 add_buffer(&buffer
, &size
, "parent %s\n", sha1_to_hex(parent_sha1
[i
]));
171 /* Person/date information */
172 add_buffer(&buffer
, &size
, "author %s <%s> %s\n", gecos
, email
, date
);
173 add_buffer(&buffer
, &size
, "committer %s <%s> %s\n\n", commitgecos
, commitemail
, realdate
);
175 /* And add the comment */
176 while (fgets(comment
, sizeof(comment
), stdin
) != NULL
)
177 add_buffer(&buffer
, &size
, "%s", comment
);
179 write_sha1_file(buffer
, size
, "commit", commit_sha1
);
180 printf("%s\n", sha1_to_hex(commit_sha1
));