static char * compare_names(const struct file_name *names, bool assume_exists, int phase) { size_t min_components, min_baselen, min_len, tmp; char *best = NULL; char *path; int i; /* * The "best" name is the one with the fewest number of path * components, the shortest basename length, and the shortest * overall length (in that order). We only use the Index: file * if neither of the old or new files could be intuited from * the diff header. */ min_components = min_baselen = min_len = SIZE_MAX; for (i = INDEX_FILE; i >= OLD_FILE; i--) { path = names[i].path; if (path == NULL || (phase == 1 && !names[i].exists && !assume_exists) || (phase == 2 && checked_in(path) == NULL)) continue; if ((tmp = num_components(path)) > min_components) continue; if (tmp < min_components) { min_components = tmp; best = path; } if ((tmp = strlen(basename(path))) > min_baselen) continue; if (tmp < min_baselen) { min_baselen = tmp; best = path; } if ((tmp = strlen(path)) > min_len) continue; min_len = tmp; best = path; } return best; }
/* * Choose the name of the file to be patched based on POSIX rules. * NOTE: the POSIX rules are amazingly stupid and we only follow them * if the user specified --posix or set POSIXLY_CORRECT. */ static char * posix_name(const struct file_name *names, bool assume_exists) { char *path = NULL; int i; /* * POSIX states that the filename will be chosen from one * of the old, new and index names (in that order) if * the file exists relative to CWD after -p stripping. */ for (i = 0; i < MAX_FILE; i++) { if (names[i].path != NULL && names[i].exists) { path = names[i].path; break; } } if (path == NULL && !assume_exists) { /* * No files found, look for something we can checkout from * RCS/SCCS dirs. Same order as above. */ for (i = 0; i < MAX_FILE; i++) { if (names[i].path != NULL && (path = checked_in(names[i].path)) != NULL) break; } /* * Still no match? Check to see if the diff could be creating * a new file. */ if (path == NULL && ok_to_create_file && names[NEW_FILE].path != NULL) path = names[NEW_FILE].path; } return path ? savestr(path) : NULL; }
/* * Choose the name of the file to be patched based the "best" one * available. */ static char * best_name(const struct file_name *names, bool assume_exists) { size_t min_components, min_baselen, min_len, tmp; char *best = NULL; int i; /* * The "best" name is the one with the fewest number of path * components, the shortest basename length, and the shortest * overall length (in that order). We only use the Index: file * if neither of the old or new files could be intuited from * the diff header. */ min_components = min_baselen = min_len = SIZE_MAX; for (i = INDEX_FILE; i >= OLD_FILE; i--) { if (names[i].path == NULL || (!names[i].exists && !assume_exists)) continue; if ((tmp = num_components(names[i].path)) > min_components) continue; min_components = tmp; if ((tmp = strlen(basename(names[i].path))) > min_baselen) continue; min_baselen = tmp; if ((tmp = strlen(names[i].path)) > min_len) continue; min_len = tmp; best = names[i].path; } if (best == NULL) { /* * No files found, look for something we can checkout from * RCS/SCCS dirs. Logic is identical to that above... */ min_components = min_baselen = min_len = SIZE_MAX; for (i = INDEX_FILE; i >= OLD_FILE; i--) { if (names[i].path == NULL || checked_in(names[i].path) == NULL) continue; if ((tmp = num_components(names[i].path)) > min_components) continue; min_components = tmp; if ((tmp = strlen(basename(names[i].path))) > min_baselen) continue; min_baselen = tmp; if ((tmp = strlen(names[i].path)) > min_len) continue; min_len = tmp; best = names[i].path; } /* * Still no match? Check to see if the diff could be creating * a new file. */ if (best == NULL && ok_to_create_file && names[NEW_FILE].path != NULL) best = names[NEW_FILE].path; } return best ? savestr(best) : NULL; }