// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.reviewdb.client;


/** Constants and utilities for Gerrit-specific ref names. */
public class RefNames {
  public static final String REFS = "refs/";

  public static final String REFS_HEADS = "refs/heads/";

  public static final String REFS_CHANGES = "refs/changes/";

  /** Note tree listing commits we refuse {@code refs/meta/reject-commits} */
  public static final String REFS_REJECT_COMMITS = "refs/meta/reject-commits";

  /** Configuration settings for a project {@code refs/meta/config} */
  public static final String REFS_CONFIG = "refs/meta/config";

  /** Preference settings for a user {@code refs/users} */
  public static final String REFS_USERS = "refs/users/";

  /** Default user preference settings */
  public static final String REFS_USERS_DEFAULT = RefNames.REFS_USERS + "default";

  /** Configurations of project-specific dashboards (canned search queries). */
  public static final String REFS_DASHBOARDS = "refs/meta/dashboards/";

  public static final String REFS_DRAFT_COMMENTS = "refs/draft-comments/";

  /**
   * Prefix applied to merge commit base nodes.
   * <p>
   * References in this directory should take the form
   * {@code refs/cache-automerge/xx/yyyy...} where xx is
   * the first two digits of the merge commit's object
   * name, and yyyyy... is the remaining 38. The reference
   * should point to a treeish that is the automatic merge
   * result of the merge commit's parents.
   */
  public static final String REFS_CACHE_AUTOMERGE = "refs/cache-automerge/";

  /** Suffix of a meta ref in the notedb. */
  public static final String META_SUFFIX = "/meta";

  public static final String EDIT_PREFIX = "edit-";

  public static String fullName(String ref) {
    return ref.startsWith(REFS) ? ref : REFS_HEADS + ref;
  }

  public static final String shortName(String ref) {
    return ref.startsWith(REFS_HEADS)
        ? ref.substring(REFS_HEADS.length())
        : ref;
  }

  public static String refsUsers(Account.Id accountId) {
    StringBuilder r = new StringBuilder();
    r.append(REFS_USERS);
    int account = accountId.get();
    int m = account % 100;
    if (m < 10) {
      r.append('0');
    }
    r.append(m);
    r.append('/');
    r.append(account);
    return r.toString();
  }

  public static String refsDraftComments(Account.Id accountId,
      Change.Id changeId) {
    StringBuilder r = new StringBuilder();
    r.append(REFS_DRAFT_COMMENTS);
    int n = accountId.get() % 100;
    if (n < 10) {
      r.append('0');
    }
    r.append(n);
    r.append('/');
    r.append(accountId.get());
    r.append('-');
    r.append(changeId.get());
    return r.toString();
  }

  /**
   * Returns reference for this change edit with sharded user and change number:
   * refs/users/UU/UUUU/edit-CCCC/P.
   *
   * @param accountId account id
   * @param changeId change number
   * @param psId patch set number
   * @return reference for this change edit
   */
  public static String refsEdit(Account.Id accountId, Change.Id changeId,
      PatchSet.Id psId) {
    return refsEditPrefix(accountId, changeId) + psId.get();
  }

  /**
   * Returns reference prefix for this change edit with sharded user and
   * change number: refs/users/UU/UUUU/edit-CCCC/.
   *
   * @param accountId account id
   * @param changeId change number
   * @return reference prefix for this change edit
   */
  public static String refsEditPrefix(Account.Id accountId, Change.Id changeId) {
    return new StringBuilder(refsUsers(accountId))
      .append('/')
      .append(EDIT_PREFIX)
      .append(changeId.get())
      .append('/')
      .toString();
  }

  private RefNames() {
  }
}

@@ -833,24 +833,80 @@
 s/heads/%22;%0A%0A
+  public static final String REFS_TAGS = %22refs/tags/%22;%0A%0A
   public sta
@@ -2504,39 +2504,130 @@
 ring ref) %7B%0A    
-return 
+if (ref.startsWith(REFS_HEADS)) %7B%0A      return ref.substring(REFS_HEADS.length());%0A    %7D else if (
 ref.startsWith(R
@@ -2634,24 +2634,29 @@
 EFS_
-HEADS)
+TAGS)) %7B
 %0A      
-  ?
+return
  ref
@@ -2663,36 +2663,35 @@
 .substring(REFS_
-HEAD
+TAG
 S.length())%0A    
@@ -2689,18 +2689,26 @@
 h())
+;
 %0A    
+%7D%0A
     
-:
+return
  ref

Add support for refs/tags/ in shortName()