• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

devonfw / IDEasy / 22072399074

16 Feb 2026 05:34PM UTC coverage: 70.465% (+0.02%) from 70.446%
22072399074

push

github

web-flow
#1699 verify input git url (#1700)

Co-authored-by: Jörg Hohwiller <hohwille@users.noreply.github.com>

4047 of 6328 branches covered (63.95%)

Branch coverage included in aggregate %.

10492 of 14305 relevant lines covered (73.34%)

3.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.72
cli/src/main/java/com/devonfw/tools/ide/git/GitUrl.java
1
package com.devonfw.tools.ide.git;
2

3
/**
4
 * Handles parsing of git URLs.
5
 *
6
 * @param url the git url e.g. https://github.com/devonfw/ide-urls.git.
7
 * @param branch the branch name e.g. master.
8
 */
9
public record GitUrl(String url, String branch) {
1✔
10

11
  /** {@link #branch() Branch} @{@value }. */
12
  public static final String BRANCH_MAIN = "main";
13

14
  /** {@link #branch() Branch} @{@value }. */
15
  public static final String BRANCH_MASTER = "master";
16

17
  /**
18
   * The constructor.
19
   */
20
  public GitUrl(String url, String branch) {
2✔
21
    this.url = url;
3✔
22
    this.branch = branch;
3✔
23
    if (url.contains("#")) {
4✔
24
      String message = "Invalid git URL " + url;
3✔
25
      assert false : message;
6✔
26
    }
27
    if (!isValid()) {
3✔
28
      assert false : "Invalid git URL - has to start with https, http or ssh: " + url;
7✔
29
    }
30
  }
1✔
31

32
  /**
33
   * Converts the Git URL based on the specified {@link GitUrlSyntax}.
34
   *
35
   * @param syntax the preferred {@link GitUrlSyntax} (SSH or HTTPS).
36
   * @return the converted {@link GitUrl} or the original if no conversion is required.
37
   */
38
  public GitUrl convert(GitUrlSyntax syntax) {
39
    return syntax.format(this);
4✔
40
  }
41

42
  /**
43
   * Extracts the project name from an git URL. For URLs like "https://github.com/devonfw/ide-urls.git" returns "ide-urls"
44
   *
45
   * @return the project name without ".git" extension
46
   */
47
  public String getProjectName() {
48

49
    int lastSlash = this.url.lastIndexOf('/');
5✔
50
    String path;
51
    if (lastSlash >= 0) {
2!
52
      path = this.url.substring(lastSlash + 1);
8✔
53
    } else {
54
      path = this.url; // actually invalid URL
×
55
    }
56
    if (path.endsWith(".git")) {
4✔
57
      path = path.substring(0, path.length() - 4);
8✔
58
    }
59
    return path;
2✔
60
  }
61

62
  /**
63
   * @return {@code true} if the URL starts with http://, https://, ssh:// or git@ - otherwise returns {@code false}.
64
   */
65
  public boolean isValid() {
66
    if (url == null || url.isBlank()) {
7!
67
      return false;
×
68
    }
69
    return url.startsWith("http://") || url.startsWith("https://") || url.startsWith("ssh://") || url.startsWith("git@");
24!
70
  }
71

72
  @Override
73
  public String toString() {
74

75
    if (this.branch == null) {
3✔
76
      return this.url;
3✔
77
    }
78
    return this.url + "#" + this.branch;
6✔
79
  }
80

81
  /**
82
   * @param gitUrl the {@link #toString() string representation} of a {@link GitUrl}. May contain a branch name as {@code «url»#«branch»}.
83
   * @return the parsed {@link GitUrl}.
84
   */
85
  public static GitUrl of(String gitUrl) {
86

87
    int hashIndex = gitUrl.indexOf('#');
4✔
88
    String url = gitUrl;
2✔
89
    String branch = null;
2✔
90
    if (hashIndex > 0) {
2✔
91
      url = gitUrl.substring(0, hashIndex);
5✔
92
      branch = gitUrl.substring(hashIndex + 1);
6✔
93
    }
94
    return new GitUrl(url, branch);
6✔
95
  }
96

97
  /**
98
   * @param gitUrl the git {@link #url() URL}.
99
   * @return a new instance of {@link GitUrl} with the given URL and {@link #BRANCH_MAIN}.
100
   */
101
  public static GitUrl ofMain(String gitUrl) {
102

103
    return new GitUrl(gitUrl, BRANCH_MAIN);
6✔
104
  }
105

106
  /**
107
   * @param gitUrl the git {@link #url() URL}.
108
   * @return a new instance of {@link GitUrl} with the given URL and {@link #BRANCH_MASTER}.
109
   */
110
  public static GitUrl ofMaster(String gitUrl) {
111

112
    return new GitUrl(gitUrl, BRANCH_MASTER);
×
113
  }
114
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc