checkout_entry(): use the strbuf throughout the function
There is no need to break out the "buf" and "len" members into separate temporary variables. Rename path_buf to path and use path.buf and path.len directly. This makes it easier to reason about the data flow in the function. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Michael Haggerty committed
Mar 13, 2014 at 10:19 UTC
f63272a35e03a9895c468d1a698dabaa4c3d9273
1 file changed
+16
-16
entry.c
+16
-16
@@ -237,27 +237,25 @@ static int check_path(const char *path, int len, struct stat *st, int skiplen)
237
int checkout_entry(struct cache_entry *ce,
238
const struct checkout *state, char *topath)
239
{
240
- static struct strbuf path_buf = STRBUF_INIT;
241
- char *path;
240
+ static struct strbuf path = STRBUF_INIT;
241
struct stat st;
243
- int len;
242
243
if (topath)
244
return write_entry(ce, topath, state, 1);
245
248
- strbuf_reset(&path_buf);
249
- strbuf_add(&path_buf, state->base_dir, state->base_dir_len);
250
- strbuf_add(&path_buf, ce->name, ce_namelen(ce));
251
- path = path_buf.buf;
252
- len = path_buf.len;
246
+ strbuf_reset(&path);
247
+ strbuf_add(&path, state->base_dir, state->base_dir_len);
248
+ strbuf_add(&path, ce->name, ce_namelen(ce));
249
254
- if (!check_path(path, len, &st, state->base_dir_len)) {
250
+ if (!check_path(path.buf, path.len, &st, state->base_dir_len)) {
251
unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
252
if (!changed)
253
return 0;
254
if (!state->force) {
255
if (!state->quiet)
260
- fprintf(stderr, "%s already exists, no checkout\n", path);
256
+ fprintf(stderr,
257
+ "%s already exists, no checkout\n",
258
+ path.buf);
259
return -1;
260
}
261
@@ -272,12 +270,14 @@ int checkout_entry(struct cache_entry *ce,
270
if (S_ISGITLINK(ce->ce_mode))
271
return 0;
272
if (!state->force)
275
- return error("%s is a directory", path);
276
- remove_subtree(path);
277
- } else if (unlink(path))
278
- return error("unable to unlink old '%s' (%s)", path, strerror(errno));
273
+ return error("%s is a directory", path.buf);
274
+ remove_subtree(path.buf);
275
+ } else if (unlink(path.buf))
276
+ return error("unable to unlink old '%s' (%s)",
277
+ path.buf, strerror(errno));
278
} else if (state->not_new)
279
return 0;
281
- create_directories(path, len, state);
282
- return write_entry(ce, path, state, 0);
280
+
281
+ create_directories(path.buf, path.len, state);
282
+ return write_entry(ce, path.buf, state, 0);
283
}