Merge branch 'ep/varscope'

Shrink lifetime of variables by moving their definitions to an
inner scope where appropriate.

* ep/varscope:
  builtin/gc.c: reduce scope of variables
  builtin/fetch.c: reduce scope of variable
  builtin/commit.c: reduce scope of variables
  builtin/clean.c: reduce scope of variable
  builtin/blame.c: reduce scope of variables
  builtin/apply.c: reduce scope of variables
  bisect.c: reduce scope of variable
This commit is contained in:
Junio C Hamano
2014-02-27 14:01:30 -08:00
7 changed files with 22 additions and 19 deletions

View File

@ -685,7 +685,6 @@ static void mark_expected_rev(char *bisect_rev_hex)
static int bisect_checkout(char *bisect_rev_hex, int no_checkout) static int bisect_checkout(char *bisect_rev_hex, int no_checkout)
{ {
int res;
mark_expected_rev(bisect_rev_hex); mark_expected_rev(bisect_rev_hex);
@ -696,6 +695,7 @@ static int bisect_checkout(char *bisect_rev_hex, int no_checkout)
die("update-ref --no-deref HEAD failed on %s", die("update-ref --no-deref HEAD failed on %s",
bisect_rev_hex); bisect_rev_hex);
} else { } else {
int res;
res = run_command_v_opt(argv_checkout, RUN_GIT_CMD); res = run_command_v_opt(argv_checkout, RUN_GIT_CMD);
if (res) if (res)
exit(res); exit(res);

View File

@ -1943,13 +1943,7 @@ static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
size - offset - hdrsize, patch); size - offset - hdrsize, patch);
if (!patchsize) { if (!patchsize) {
static const char *binhdr[] = {
"Binary files ",
"Files ",
NULL,
};
static const char git_binary[] = "GIT binary patch\n"; static const char git_binary[] = "GIT binary patch\n";
int i;
int hd = hdrsize + offset; int hd = hdrsize + offset;
unsigned long llen = linelen(buffer + hd, size - hd); unsigned long llen = linelen(buffer + hd, size - hd);
@ -1965,6 +1959,12 @@ static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
patchsize = 0; patchsize = 0;
} }
else if (!memcmp(" differ\n", buffer + hd + llen - 8, 8)) { else if (!memcmp(" differ\n", buffer + hd + llen - 8, 8)) {
static const char *binhdr[] = {
"Binary files ",
"Files ",
NULL,
};
int i;
for (i = 0; binhdr[i]; i++) { for (i = 0; binhdr[i]; i++) {
int len = strlen(binhdr[i]); int len = strlen(binhdr[i]);
if (len < size - hd && if (len < size - hd &&

View File

@ -1580,14 +1580,14 @@ static const char *format_time(unsigned long time, const char *tz_str,
int show_raw_time) int show_raw_time)
{ {
static char time_buf[128]; static char time_buf[128];
const char *time_str;
int time_len;
int tz;
if (show_raw_time) { if (show_raw_time) {
snprintf(time_buf, sizeof(time_buf), "%lu %s", time, tz_str); snprintf(time_buf, sizeof(time_buf), "%lu %s", time, tz_str);
} }
else { else {
const char *time_str;
int time_len;
int tz;
tz = atoi(tz_str); tz = atoi(tz_str);
time_str = show_date(time, tz, blame_date_mode); time_str = show_date(time, tz, blame_date_mode);
time_len = strlen(time_str); time_len = strlen(time_str);

View File

@ -154,7 +154,7 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
DIR *dir; DIR *dir;
struct strbuf quoted = STRBUF_INIT; struct strbuf quoted = STRBUF_INIT;
struct dirent *e; struct dirent *e;
int res = 0, ret = 0, gone = 1, original_len = path->len, len, i; int res = 0, ret = 0, gone = 1, original_len = path->len, len;
unsigned char submodule_head[20]; unsigned char submodule_head[20];
struct string_list dels = STRING_LIST_INIT_DUP; struct string_list dels = STRING_LIST_INIT_DUP;
@ -242,6 +242,7 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
} }
if (!*dir_gone && !quiet) { if (!*dir_gone && !quiet) {
int i;
for (i = 0; i < dels.nr; i++) for (i = 0; i < dels.nr; i++)
printf(dry_run ? _(msg_would_remove) : _(msg_remove), dels.items[i].string); printf(dry_run ? _(msg_would_remove) : _(msg_remove), dels.items[i].string);
} }

View File

@ -307,7 +307,6 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
int fd; int fd;
struct string_list partial; struct string_list partial;
struct pathspec pathspec; struct pathspec pathspec;
char *old_index_env = NULL;
int refresh_flags = REFRESH_QUIET; int refresh_flags = REFRESH_QUIET;
if (is_status) if (is_status)
@ -320,6 +319,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
die(_("index file corrupt")); die(_("index file corrupt"));
if (interactive) { if (interactive) {
char *old_index_env = NULL;
fd = hold_locked_index(&index_lock, 1); fd = hold_locked_index(&index_lock, 1);
refresh_cache_or_die(refresh_flags); refresh_cache_or_die(refresh_flags);
@ -600,12 +600,10 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
{ {
struct stat statbuf; struct stat statbuf;
struct strbuf committer_ident = STRBUF_INIT; struct strbuf committer_ident = STRBUF_INIT;
int commitable, saved_color_setting; int commitable;
struct strbuf sb = STRBUF_INIT; struct strbuf sb = STRBUF_INIT;
char *buffer;
const char *hook_arg1 = NULL; const char *hook_arg1 = NULL;
const char *hook_arg2 = NULL; const char *hook_arg2 = NULL;
int ident_shown = 0;
int clean_message_contents = (cleanup_mode != CLEANUP_NONE); int clean_message_contents = (cleanup_mode != CLEANUP_NONE);
int old_display_comment_prefix; int old_display_comment_prefix;
@ -649,6 +647,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
logfile); logfile);
hook_arg1 = "message"; hook_arg1 = "message";
} else if (use_message) { } else if (use_message) {
char *buffer;
buffer = strstr(use_message_buffer, "\n\n"); buffer = strstr(use_message_buffer, "\n\n");
if (!use_editor && (!buffer || buffer[2] == '\0')) if (!use_editor && (!buffer || buffer[2] == '\0'))
die(_("commit has empty message")); die(_("commit has empty message"));
@ -753,6 +752,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
/* This checks if committer ident is explicitly given */ /* This checks if committer ident is explicitly given */
strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT)); strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT));
if (use_editor && include_status) { if (use_editor && include_status) {
int ident_shown = 0;
int saved_color_setting;
char *ai_tmp, *ci_tmp; char *ai_tmp, *ci_tmp;
if (whence != FROM_COMMIT) if (whence != FROM_COMMIT)
status_printf_ln(s, GIT_COLOR_NORMAL, status_printf_ln(s, GIT_COLOR_NORMAL,
@ -1514,7 +1515,6 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
struct ref_lock *ref_lock; struct ref_lock *ref_lock;
struct commit_list *parents = NULL, **pptr = &parents; struct commit_list *parents = NULL, **pptr = &parents;
struct stat statbuf; struct stat statbuf;
int allow_fast_forward = 1;
struct commit *current_head = NULL; struct commit *current_head = NULL;
struct commit_extra_header *extra = NULL; struct commit_extra_header *extra = NULL;
@ -1562,6 +1562,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
} else if (whence == FROM_MERGE) { } else if (whence == FROM_MERGE) {
struct strbuf m = STRBUF_INIT; struct strbuf m = STRBUF_INIT;
FILE *fp; FILE *fp;
int allow_fast_forward = 1;
if (!reflog_msg) if (!reflog_msg)
reflog_msg = "commit (merge)"; reflog_msg = "commit (merge)";

View File

@ -1026,7 +1026,6 @@ static int fetch_multiple(struct string_list *list)
static int fetch_one(struct remote *remote, int argc, const char **argv) static int fetch_one(struct remote *remote, int argc, const char **argv)
{ {
int i;
static const char **refs = NULL; static const char **refs = NULL;
struct refspec *refspec; struct refspec *refspec;
int ref_nr = 0; int ref_nr = 0;
@ -1050,6 +1049,7 @@ static int fetch_one(struct remote *remote, int argc, const char **argv)
if (argc > 0) { if (argc > 0) {
int j = 0; int j = 0;
int i;
refs = xcalloc(argc + 1, sizeof(const char *)); refs = xcalloc(argc + 1, sizeof(const char *));
for (i = 0; i < argc; i++) { for (i = 0; i < argc; i++) {
if (!strcmp(argv[i], "tag")) { if (!strcmp(argv[i], "tag")) {

View File

@ -188,13 +188,12 @@ static int need_to_gc(void)
static const char *lock_repo_for_gc(int force, pid_t* ret_pid) static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
{ {
static struct lock_file lock; static struct lock_file lock;
static char locking_host[128];
char my_host[128]; char my_host[128];
struct strbuf sb = STRBUF_INIT; struct strbuf sb = STRBUF_INIT;
struct stat st; struct stat st;
uintmax_t pid; uintmax_t pid;
FILE *fp; FILE *fp;
int fd, should_exit; int fd;
if (pidfile) if (pidfile)
/* already locked */ /* already locked */
@ -206,6 +205,8 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
fd = hold_lock_file_for_update(&lock, git_path("gc.pid"), fd = hold_lock_file_for_update(&lock, git_path("gc.pid"),
LOCK_DIE_ON_ERROR); LOCK_DIE_ON_ERROR);
if (!force) { if (!force) {
static char locking_host[128];
int should_exit;
fp = fopen(git_path("gc.pid"), "r"); fp = fopen(git_path("gc.pid"), "r");
memset(locking_host, 0, sizeof(locking_host)); memset(locking_host, 0, sizeof(locking_host));
should_exit = should_exit =