};
struct refname_atom {
- enum { R_NORMAL, R_SHORT, R_STRIP } option;
- unsigned int strip;
+ enum { R_NORMAL, R_SHORT, R_LSTRIP, R_RSTRIP } option;
+ int lstrip, rstrip;
};
/*
atom->option = R_NORMAL;
else if (!strcmp(arg, "short"))
atom->option = R_SHORT;
- else if (skip_prefix(arg, "strip=", &arg)) {
- atom->option = R_STRIP;
- if (strtoul_ui(arg, 10, &atom->strip) || atom->strip <= 0)
- die(_("positive value expected refname:strip=%s"), arg);
+ else if (skip_prefix(arg, "lstrip=", &arg)) {
+ atom->option = R_LSTRIP;
+ if (strtol_i(arg, 10, &atom->lstrip))
+ die(_("Integer value expected refname:lstrip=%s"), arg);
+ } else if (skip_prefix(arg, "rstrip=", &arg)) {
+ atom->option = R_RSTRIP;
+ if (strtol_i(arg, 10, &atom->rstrip))
+ die(_("Integer value expected refname:rstrip=%s"), arg);
} else
die(_("unrecognized %%(%s) argument: %s"), name, arg);
}
return dst;
}
-static const char *strip_ref_components(const char *refname, unsigned int len)
+static const char *lstrip_ref_components(const char *refname, int len)
{
long remaining = len;
const char *start = refname;
- while (remaining) {
+ if (len < 0) {
+ int i;
+ const char *p = refname;
+
+ /* Find total no of '/' separated path-components */
+ for (i = 0; p[i]; p[i] == '/' ? i++ : *p++)
+ ;
+ /*
+ * The number of components we need to strip is now
+ * the total minus the components to be left (Plus one
+ * because we count the number of '/', but the number
+ * of components is one more than the no of '/').
+ */
+ remaining = i + len + 1;
+ }
+
+ while (remaining > 0) {
switch (*start++) {
case '\0':
- die(_("ref '%s' does not have %ud components to :strip"),
- refname, len);
+ return "";
case '/':
remaining--;
break;
}
}
+
+ return start;
+}
+
+static const char *rstrip_ref_components(const char *refname, int len)
+{
+ long remaining = len;
+ char *start = xstrdup(refname);
+
+ if (len < 0) {
+ int i;
+ const char *p = refname;
+
+ /* Find total no of '/' separated path-components */
+ for (i = 0; p[i]; p[i] == '/' ? i++ : *p++)
+ ;
+ /*
+ * The number of components we need to strip is now
+ * the total minus the components to be left (Plus one
+ * because we count the number of '/', but the number
+ * of components is one more than the no of '/').
+ */
+ remaining = i + len + 1;
+ }
+
+ while (remaining-- > 0) {
+ char *p = strrchr(start, '/');
+ if (p == NULL)
+ return "";
+ else
+ p[0] = '\0';
+ }
return start;
}
{
if (atom->option == R_SHORT)
return shorten_unambiguous_ref(refname, warn_ambiguous_refs);
- else if (atom->option == R_STRIP)
- return strip_ref_components(refname, atom->strip);
+ else if (atom->option == R_LSTRIP)
+ return lstrip_ref_components(refname, atom->lstrip);
+ else if (atom->option == R_RSTRIP)
+ return rstrip_ref_components(refname, atom->rstrip);
else
return refname;
}